package ti;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;

/**
 * The Parser initiates and, depending on the mode, disassembles a binary or text input file 
 * and converts it to the other.
 * 
 * @author Nicola Buescher
 */
public class Parser {
    
    /*-----------------------*
     * Variable Declarations *
     *-----------------------*/
	private ArrayList<Token> tests;
    private static final String l = System.getProperty("file.separator");
    private static final String WORKING_DIR = System.getProperty("java.class.path");
    private static final String USER_DIR = System.getProperty("user.home");
    private static final String tokenFiller = "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ";
    
    /**
     * Initiates the parser in the given mode. This is when parsing actually begins.
     * 
     * @param mode the mode of the Parser (either "TXT" or "8XP")
     * @return void
     * @throws ParserModeException thrown when the parser is initialized in an invalid mode
     */
    public void initiate(String mode) throws ParserModeException, FileNotFoundException{
    	buildTokenLibrary();
    	if (mode.equalsIgnoreCase("TXT")) {
    		
    	} else if (mode.equalsIgnoreCase("8XP")) {
    		
    	} else {
    		throw new ParserModeException(mode);
    	}
    }
    
    /**
     * Parses the specified 8XP file into plain text (in UTF-8 encoding).
     * 
     * @param file the file from which the data will be drawn
     * @return void
     */
    private void parse8XP(File file) {
        
    }
    
    /**
     * Parses the specified TXT file into an 8XP file to be transferred to the calculator.
     * 
     * @param file the file from which the data will be drawn
     * @return void
     */
    private void parseTXT(File file) {
        
    }
    
    /**
     * Builds a library of tokens that will be used to parse the text input or to de-parse the 
     *     binary input file.
     *     
     * @return void
     */
    private void buildTokenLibrary() throws FileNotFoundException {
        
        /* IDEA Replace "buildTokenLibrary" method with a read-from-file method that reads the
         * valid tokens from a file
         */
        
        // TODO Flesh out; add in the tokens
        BufferedReader inputTestTokens = new BufferedReader(new FileReader(new File("TIBASIC.tkns")));
    }
    
    /**
     * The Token class to help parse the input.
     * TI-BASIC works in tokens, where each token is a one-to-one match to an ASM command, so
     *     this class stores and handles all of the relevant data associated with a token.
     */
    private static class Token {
        
        /*-----------------------*
         * Variable Declarations *
         *-----------------------*/
        public String text;
        public int[] value;
        
        /**
         * Constructor; instantiates a new one-byte token.
         * @param text the text representation of the token
         * @param value the one-byte representation of the token
         */
        public Token(String text, int value) {
            this.text = text;
            this.value = new int[1];
            this.value[0] = value;
        }
        
        /**
         * Constructor; instantiates a new two-byte token.
         * 
         * @param text the text representation of the token
         * @param hi the high byte of the two-byte representation of the token
         * @param lo the low byte of the two-byte representation of the token
         */
        public Token(String text, int hi, int lo) {
            this.text = text;
            this.value = new int[2];
            this.value[0] = hi;
            this.value[1] = lo;
        }
    }
    
    /**
     * An Exception that is thrown when the mode that the parser is initiated with does not
     * correspond with either of the two valid modes.
     * 
     * @author Nicola Buescher
     */
    static class ParserModeException extends Exception {
    	
    	/**
    	 * Constructs the new Exception to report why the exception was thrown.
    	 * 
    	 * @param mode the mode that the parser was initialized with and failed on
    	 */
    	public ParserModeException(String mode) {
    		super("\"" + mode + "\" is not a valid mode!");
    	}
    }
    
    /**
     * Main method for debugging purposes.
     * 
     * @param args the arguments passed by the command line
     */
    public static void main(String[] args) {
        
    }
}